//
//  EidtTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/10.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "EidtTableViewCell.h"
#import "Pubmode.h"
#define NUMBERS @"0123456789.n"
@interface EidtTableViewCell ()<UITextFieldDelegate>

@end

@implementation EidtTableViewCell

+(instancetype)cellWithTable:(UITableView *)tableView{
    static NSString *ID=@"editcell";
    EidtTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellReuseIdentifier:ID];
        cell=[tableView dequeueReusableCellWithIdentifier:ID];

    }
    return cell;
}
-(void)setMode:(Pubmode *)mode{
    _mode=mode;
    self.titlelable.text=_mode.name;
    self.ismust.hidden=!_mode.isMust;
    self.detail.text=_mode.detail;
    if (_mode.pricetitle) {
        self.price.text=_mode.pricetitle;
    }
    if (_mode.yuancolor) {
        self.price.textColor=_mode.yuancolor;
    }else {
        self.price.textColor=textnormalGary;
    }
    if (_mode.placehoder) {
        self.detail.placeholder=_mode.placehoder;
    }else{
        self.detail.placeholder=@"请输入";
    }
    if (_mode.isAluto) {
        self.detail.enabled=NO;
    }else{
        self.detail.enabled=YES;
    }
    
}
- (void)awakeFromNib {
    // Initialization code
    self.detail.delegate=self;
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    BOOL isHaveDian=NO;
    
    if (textField.keyboardType==UIKeyboardTypeDecimalPad) {
        if ([textField.text rangeOfString:@"."].location!=NSNotFound) {
            isHaveDian=YES;
        }
        if ([string length]>0)
        {
            unichar single=[string characterAtIndex:0];//当前输入的字符
            if ((single >='0' && single<='9') || single=='.')//数据格式正确
            {
                //首字母不能为0和小数点
                if([textField.text length]==0){
                    if(single == '.'){
                        [self alertView:@"亲，第一个数字不能为小数点"];
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
//                    if (single == '0') {
//                        [self alertView:@"亲，第一个数字不能为0"];
//                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
//                        return NO;
//                        
//                    }
                }
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        [self alertView:@"亲，您已经输入过小数点了"];
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            [self alertView:@"亲，您最多输入两位小数"];
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }else{//输入的数据格式不正确
                [self alertView:@"亲，您输入的格式不正确"];  
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;  
            }  
        }  
        else  
        {  
            return YES;  
        }  
        
    

    }else{
        return YES
        ;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (_mode.limitlength!=0) {
        if (([textField.text containsString:@"."]&&[textField.text rangeOfString:@"."].location<_mode.limitlength)||(![textField.text containsString:@"."]&&textField.text.length<_mode.limitlength)) {
            [self alertView:[NSString stringWithFormat:@"整数位不能少于%ld位",_mode.limitlength]];
            textField.text=@"";
            return;
        }
    }
    self.mode.detail=textField.text;
    if ([_delegate respondsToSelector:@selector(textFieldChange:)]) {
        [_delegate textFieldChange:self];
    }
}
-(void)alertView:(NSString *)string{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if ([_delegate respondsToSelector:@selector(textFieldBeginEidtChange:withTextFiled:)]) {
        [_delegate textFieldBeginEidtChange:self withTextFiled:self.detail];
    }

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
